import { reqGetUserInfo, reqGetUserInfoMsg, reqLogin, reqSendCode } from '@/api/user'
import { LoginForm, UserInfo, UserInfoMsg } from '@/types/user'
import { defineStore } from 'pinia'
const useUserStore = defineStore('user',
    {
        state: () => {
            return {
                tokenInfo: {} as {
                    token: string,
                    refresh_token: string
                },
                userInfo: {} as UserInfo || {},
                userInfoList: {} as UserInfoMsg || {}
            }
        },
        actions: {
            // 发送验证码
            async sendCode(mobile: string) {
                await reqSendCode(mobile)
            },
            // 登录
            async toLogin(LoginForm: LoginForm) {
                const { data } = await reqLogin(LoginForm)
                this.tokenInfo = data.data
                const res = await reqGetUserInfo()
                this.userInfo = res.data.data
            },
            // 获取用户个人信息
            async getUserInfo() {
                const { data } = await reqGetUserInfoMsg()
                this.userInfoList = data.data
            },
            // 再次获取用户基本信息
            async resetUserInfo() {
                const res = await reqGetUserInfo()
                this.userInfo = res.data.data
            },
            // 退出登录
            loginOut() {
                this.tokenInfo = {} as {
                    token: string,
                    refresh_token: string
                }
                this.userInfo = {} as UserInfo
            }
        },
        getters: {

        },
        persist: {
            enabled: true,
            strategies: [{
                storage: localStorage,
                paths: ['tokenInfo', 'userInfo']
            }]
        }
    },)
export default useUserStore